package com.xydtech.ecard.ftp.service.impl;

import com.xydtech.common.exception.ServiceException;
import com.xydtech.common.utils.DateUtils;
import com.xydtech.ecard.ftp.domain.EcardApiFtpInfo;
import com.xydtech.ecard.ftp.domain.EcardFtpResultsSummaryBO;
import com.xydtech.ecard.ftp.enums.*;
import com.xydtech.ecard.ftp.service.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: liliya
 * @Description: 电子名片-FTP
 * @Date: Created in 2023/8/15 16:34
 * @Modified:
 */
@Service
public class EcardFtpServiceImpl implements IEcardFtpService {

    private final Logger logger = LogManager.getLogger(this.getClass());

    private IEcardFtpCalculateFtpService ecardFtpCalculateFtpLoansService;
    private IEcardFtpCalculateFtpService ecardFtpCalculateFtpDepositService;
    public EcardFtpServiceImpl(IEcardFtpCalculateFtpService ecardFtpCalculateFtpLoansService,
                               IEcardFtpCalculateFtpService ecardFtpCalculateFtpDepositService) {
        this.ecardFtpCalculateFtpLoansService = ecardFtpCalculateFtpLoansService;
        this.ecardFtpCalculateFtpDepositService = ecardFtpCalculateFtpDepositService;
    }

    /**
     * 电子名片-ftp计算
     * TODO 注意小数、求值set后再get出来而不是直接用临时变量的字段
     * @param ecardApiFtpInfo
     */
    @Override
    public EcardFtpResultsSummaryBO calculateFtpEntrance(EcardApiFtpInfo ecardApiFtpInfo) {


        //TODO 计算FTP
        EcardFtpResultsSummaryBO ecardFtpResultsSummaryBO;

        logger.info("电子名片FTP计算初始化信息------------------------>{}",ecardApiFtpInfo);

        /**
         * 业务品种
         * 贷款	    1	ftp_business_variety
         * 定期存款	2	ftp_business_variety
         * 个人活期	3	ftp_business_variety
         * 单位活期	4	ftp_business_variety
         */
        Integer ftpBusinessVariety = ecardApiFtpInfo.getFtpBusinessVariety();
        FtpBusinessVarietyEnum ftpBusinessVarietyEnum = FtpBusinessVarietyEnum.values(ftpBusinessVariety);
        if(ftpBusinessVarietyEnum == null){
            throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION1.getMessage());
        }

        /**
         * 利率类型
         * 固定	1	interest_rate_type
         * 浮动	2	interest_rate_type
         */
        Integer interestRateType = ecardApiFtpInfo.getInterestRateType();
        FtpInterestRateTypeEnum ftpInterestRateTypeEnum = FtpInterestRateTypeEnum.values(interestRateType);
        if(ftpInterestRateTypeEnum == null){
            throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION2.getMessage());
        }

        /**
         * 日期间隔判断
         */
        Date beginDate = ecardApiFtpInfo.getBeginDate();
        Date endDate = ecardApiFtpInfo.getEndDate();

        int intervalDay = DateUtils.differentDaysByActualMillisecond(beginDate,endDate);
        //固定利率类型:业务到期日至少要大于业务起始日'1天'!
        if(ftpInterestRateTypeEnum.getValue() == FtpInterestRateTypeEnum.FIXED_RATE.getValue()){
            if(intervalDay <= 0){
                throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION19.getMessage());
            }
        }
        //浮动利率类型:业务到期日至少要大于业务起始日'三个月'!
        else if(ftpInterestRateTypeEnum.getValue() == FtpInterestRateTypeEnum.FLOAT_RATE.getValue()){
            if(intervalDay < 90){
                throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION20.getMessage());
            }
        }

        /**
         * 还款方式
         * TODO 存款不填此项
         * 到期还本付息	    1	loan_repayment_mode_type
         * 到期还本定期结息	2	loan_repayment_mode_type
         * 等额本金	        3	loan_repayment_mode_type
         * 等额本息	        4	loan_repayment_mode_type
         * 按还款计划还款	5	loan_repayment_mode_type
         */
        Integer loanRepaymentModeType = ecardApiFtpInfo.getLoanRepaymentModeType();
        /**
         * 还款频率(月)
         * TODO 当贷款还款方式loanRepaymentModeType为等额本金和等额本息必填
         * 1M	1	repayment_frequency_type
         * 3M	3	repayment_frequency_type
         * 6M   6	repayment_frequency_type
         * 12M	12	repayment_frequency_type
         */
        Integer repaymentFrequencyType = ecardApiFtpInfo.getRepaymentFrequencyType();
        /**
         * 结息频率(月)
         * TODO 当贷款还款方式为到期还本付息和业务品种为存款不填此项
         * 1M	    1	interest_frequency_type
         * 3M	    3	interest_frequency_type
         */
        Integer interestFrequencyType = ecardApiFtpInfo.getInterestFrequencyType();

        /**
         * 存款校验和执行方法入口部分
         */
        if(ftpBusinessVariety != FtpBusinessVarietyEnum.LOANS.getValue()){


            //TODO 存款暂不开放!
            throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION3.getMessage());

            ///**
            // * 计算FTP贷款的结果汇总信息
            // * TODO (暂无)  使用打开,删除上述抛出的异常,并放在下述所有校验最下面.
            // */
            //ecardFtpResultsSummaryBO = ecardFtpCalculateFtpDepositService.getCalculateFtpResultsInfo(ecardApiFtpInfo);


            ///**
            // * 存款必须选固定利率!
            // */
            //if(interestRateType != FtpInterestRateTypeEnum.FIXED_RATE.getValue()){
            //    //TODO 加返回错误校验内容
            //    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION16.getMessage());
            //}
            ///**
            // * 存款不填还款方式!
            // */
            //if(loanRepaymentModeType != null && FtpLoanRepaymentModeTypeEnum.values(loanRepaymentModeType) != null){
            //    //TODO 加返回错误校验内容
            //    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION17.getMessage());
            //}
            ///**
            // * 存款不填还款频率(月)!
            // */
            //if(repaymentFrequencyType != null && FtpRepaymentFrequencyTypeEnum.values(repaymentFrequencyType) != null){
            //    //TODO 加返回错误校验内容
            //    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION15.getMessage());
            //}
            ///**
            // * 如果贷款还款方式是到期还本付息或业务品种为存款,那么结息频率(月)不填!
            // */
            //if(interestFrequencyType != null && FtpInterestFrequencyTypeEnum.values(interestFrequencyType) != null){
            //    //TODO 加返回错误校验内容
            //    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION8.getMessage());
            //}

            ///**
            // * 重定价频率(月)
            // * TODO 固定利率的贷款和存款不填此项,即:固定利率无重定价频率
            // * TODO 贷款浮动利率需要有重定价频率
            // * 3M  	3	repricing_frequency_type
            // * 6M  	6	repricing_frequency_type
            // * 12M	12	repricing_frequency_type
            // */
            //Integer repricingFrequencyType = ecardApiFtpInfo.getRepricingFrequencyType();
            ////存款不填重定价频率!
            //if(repricingFrequencyType != null){
            //    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION11.getMessage());
            //}

        }
        /**
         * 贷款校验和执行方法入口部分
         */
        else{
            /**
             * 贷款必填还款方式!
             */
            if(loanRepaymentModeType == null){
                //TODO 加返回错误校验内容
                throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION4.getMessage());
            }
            FtpLoanRepaymentModeTypeEnum ftpLoanRepaymentModeTypeEnum = FtpLoanRepaymentModeTypeEnum.values(loanRepaymentModeType);
            if(ftpLoanRepaymentModeTypeEnum == null){
                //TODO 加返回错误校验内容
                throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION13.getMessage());
            }

            //TODO 贷款还款方式:是否开放!
            if(loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE5.getValue()){
                //TODO 加返回错误校验内容
                throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION5.getMessage());
            }

            //等额本金或等额本息
            boolean loadRepaymentMode = (
                    loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE3.getValue()
                            ||
                            loanRepaymentModeType == FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE4.getValue()
            );
            if(loadRepaymentMode){
                /**
                 * 还款频率(月):当贷款还款方式为等额本金和等额本息必填!
                 */
                if(repaymentFrequencyType == null){
                    //TODO 加返回错误校验内容
                    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION6.getMessage());
                }
                FtpRepaymentFrequencyTypeEnum ftpRepaymentFrequencyTypeEnum = FtpRepaymentFrequencyTypeEnum.values(repaymentFrequencyType);
                if(ftpRepaymentFrequencyTypeEnum == null){
                    //TODO 加返回错误校验内容
                    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION14.getMessage());
                }
                if(repaymentFrequencyType == FtpRepaymentFrequencyTypeEnum.REPAYMENT_FREQUENCY3.getValue() ||
                        repaymentFrequencyType == FtpRepaymentFrequencyTypeEnum.REPAYMENT_FREQUENCY4.getValue()){
                    //TODO 加返回错误校验内容
                    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION21.getMessage());
                }
                //还款频率(月):1M
                if(repaymentFrequencyType == FtpRepaymentFrequencyTypeEnum.REPAYMENT_FREQUENCY1.getValue()){
                    //1年到30年
                    if(intervalDay < 365){
                        throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION23.getMessage());
                    }
                }
                //还款频率(月):3M
                else if(repaymentFrequencyType == FtpRepaymentFrequencyTypeEnum.REPAYMENT_FREQUENCY2.getValue()){
                    //3月到2年
                    if(intervalDay < 90 || intervalDay > 731){
                        throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION22.getMessage());
                    }
                }
            }
            //TODO 暂时去掉结息频率(月)校验,小程序默认都选择值,而且不是必填项,不能清空
            ///**
            // * 结息频率(月)
            // * TODO 当贷款还款方式为到期还本付息和业务品种为存款不填此项
            // * 1M	    1	interest_frequency_type
            // * 3M	    3	interest_frequency_type
            // */
            //if(loanRepaymentModeType != FtpLoanRepaymentModeTypeEnum.LOAN_REPAYMENT_MODE1.getValue()){
            //    /**
            //     * 如果贷款还款方式不是"到期还本付息",那么结息频率(月)必填!
            //     */
            //    if(interestFrequencyType == null || FtpInterestFrequencyTypeEnum.values(interestFrequencyType) == null){
            //        //TODO 加返回错误校验内容
            //        throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION7.getMessage());
            //    }
            //}else{
            //    /**
            //     * 如果贷款还款方式是"到期还本付息"和业务品种为"存款",那么结息频率(月)不填!
            //     */
            //    if(interestFrequencyType != null){
            //        //TODO 加返回错误校验内容
            //        throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION8.getMessage());
            //    }
            //}

            /**
             * 重定价频率(月)
             * TODO 固定利率的贷款和存款不填此项,即:固定利率无重定价频率
             * TODO 贷款浮动利率需要有重定价频率
             * 3M  	3	repricing_frequency_type
             * 6M  	6	repricing_frequency_type
             * 12M	12	repricing_frequency_type
             */
            Integer repricingFrequencyType = ecardApiFtpInfo.getRepricingFrequencyType();
            if(interestRateType == FtpInterestRateTypeEnum.FIXED_RATE.getValue()){
                //TODO 暂时去掉,小程序默认都选择值,不能清空
                ////固定利率不填重定价频率!
                //if(repricingFrequencyType != null){
                //    //TODO 加返回错误校验内容
                //    throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION9.getMessage());
                //}
            }else if(interestRateType == FtpInterestRateTypeEnum.FLOAT_RATE.getValue()){

                /**
                 * 贷款浮动利率必填重定价频率!
                 */
                if(ftpBusinessVariety == FtpBusinessVarietyEnum.LOANS.getValue()){
                    if(repricingFrequencyType == null){
                        //TODO 加返回错误校验内容
                        throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION10.getMessage());
                    }
                    FtpRepricingFrequencyTypeEnum ftpRepricingFrequencyTypeEnum = FtpRepricingFrequencyTypeEnum.values(repricingFrequencyType);
                    if(ftpRepricingFrequencyTypeEnum == null){
                        //TODO 加返回错误校验内容
                        throw new ServiceException(FtpCaculateExceptionEnum.EXCEPTION10.getMessage());
                    }
                }
            }
            /**
             * 计算FTP贷款的结果汇总信息
             */
            ecardFtpResultsSummaryBO = ecardFtpCalculateFtpLoansService.getCalculateFtpResultsInfo(ecardApiFtpInfo);
        }

        return ecardFtpResultsSummaryBO;

    }

    public IEcardFtpCalculateFtpService getEcardFtpCalculateFtpLoansService() {
        return ecardFtpCalculateFtpLoansService;
    }

    public void setEcardFtpCalculateFtpLoansService(IEcardFtpCalculateFtpService ecardFtpCalculateFtpLoansService) {
        this.ecardFtpCalculateFtpLoansService = ecardFtpCalculateFtpLoansService;
    }

    public IEcardFtpCalculateFtpService getEcardFtpCalculateFtpDepositService() {
        return ecardFtpCalculateFtpDepositService;
    }

    public void setEcardFtpCalculateFtpDepositService(IEcardFtpCalculateFtpService ecardFtpCalculateFtpDepositService) {
        this.ecardFtpCalculateFtpDepositService = ecardFtpCalculateFtpDepositService;
    }

}
